#include "SettingsScene.h"
#include "MenuScene.h"
#include "GlobalDefine.h"
USING_NS_CC;

CCScene* SettingsScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    SettingsScene *layer = SettingsScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
	scene->setTag(TAG_OPTION);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool SettingsScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.
    
    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "CloseNormal.png",
                                                          "CloseSelected.png",
                                                          this,
                                                          menu_selector(SettingsScene::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2  + 5));
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);
    
    /////////////////////////////
    // 3. add your codes below...
    
    // add "SettingsScene" splash screen"
    CCSprite* pSprite = CCSprite::create("settings_scene.png");
    
    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    
    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
	// Init Slider Sound & Music
	initSliderLabel();
	initSlider();

    return true;
}


void SettingsScene::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->replaceScene(MenuScene::scene());
}

void SettingsScene::initSlider()
{
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

	// Add the slider sound
    CCControlSlider *sliderSound = CCControlSlider::create("sliderTrack.png", "sliderProgress.png" ,"sliderThumb.png");
    sliderSound->setAnchorPoint(ccp(0.5f, 0.5f));
    sliderSound->setMinimumValue(0.0f); // Sets the min value of range
    sliderSound->setMaximumValue(5.0f); // Sets the max value of range
	sliderSound->setValue(5.0f); // Sets the default value
    sliderSound->setPosition(ccp(screenSize.width * 0.5, screenSize.height * 0.6));
	sliderSound->setTag(SLIDER::SOUND);

    // When the value of the slider will change, the given selector will be call
    sliderSound->addTargetWithActionForControlEvents(this, cccontrol_selector(SettingsScene::valueChanged), CCControlEventValueChanged);

	this->addChild(sliderSound);

	//// Add the slider music
 //   CCControlSlider *sliderMusic = CCControlSlider::create("sliderTrack.png","sliderProgress.png" ,"sliderThumb.png");
 //   sliderMusic->setAnchorPoint(ccp(0.5f, 0.5f));
 //   sliderMusic->setMinimumValue(0.0f); // Sets the min value of range
 //   sliderMusic->setMaximumValue(5.0f); // Sets the max value of range
	//sliderMusic->setValue(5.0f); // Sets the default value
 //   sliderMusic->setPosition(ccp(screenSize.width * 0.5, screenSize.height * 0.4));
	//sliderMusic->setTag(SLIDER::MUSIC);

 //   // When the value of the slider will change, the given selector will be call
 //   sliderMusic->addTargetWithActionForControlEvents(this, cccontrol_selector(SettingsScene::valueChanged), CCControlEventValueChanged);

	//this->addChild(sliderMusic);
}

void SettingsScene::initSliderLabel()
{
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

	// Sound label
	CCLabelTTF* lblSound = CCLabelTTF::create("Sound" , "fonts/arial.ttf", screenSize.width*0.05);
	lblSound->setAnchorPoint(ccp(0.0f, 0.5f));
	lblSound->setPosition(ccp(screenSize.width * 0.05 + 50, screenSize.height * 0.6));
	this->addChild(lblSound);

	//// Music label
	//CCLabelTTF* lblMusic = CCLabelTTF::create("Music" , "fonts/arial.ttf", screenSize.width*0.05);
	//lblMusic->setAnchorPoint(ccp(0.0f, 0.5f));
	//lblMusic->setPosition(ccp(screenSize.width * 0.05 + 50, screenSize.height * 0.4));
	//this->addChild(lblMusic);
}

void SettingsScene::valueChanged(CCObject *sender, CCControlEvent controlEvent)
{
    CCControlSlider* pSlider = (CCControlSlider*)sender;
	if(pSlider->getTag() == SLIDER::SOUND)
	{
		if(pSlider->getValue() > 0)
			g_isSound = true;
		else
			g_isSound = false;
	}

	/*if(pSlider->getTag() == SLIDER::MUSIC)
	{
		if(pSlider->getValue() > 0)
			g_isMusic = true;
		else
			g_isMusic = false;
	}*/
}